# Initialize total within sum of squares error: wsswss <-0# For 1 to 15 cluster centersfor (i in1:15) { km.out <-kmeans(df_cl[-1], centers = i, nstart =20)# Save total within sum of squares to wss variable wss[i] <- km.out$tot.withinss}# Plot total within sum of squares vs. number of clustersplot(1:15, wss, type ="b", xlab ="Number of Clusters", ylab ="Within groups sum of squares")
# Set k equal to the number of clusters corresponding to the elbow locationk <-4
Cluster with k = 4
km.out <-kmeans(df_cl[-1], centers =4, nstart =20)df =data.frame(names =row.names(df_cl),mean_BAS = df_cl$mean_BAS,price_diff = df_cl$price_diff,cluster =factor(km.out$cluster))plot =ggplot(df, aes(x = mean_BAS, y = price_diff, color = cluster, label = names)) +geom_point() +geom_text(aes(label=names), vjust =-1, hjust =1) +theme_minimal() +labs(title ="Cluster Plot", x ="mean_BAS", y ="price_diff")library(plotly)ggplotly(plot)